Contents

Last modified: 2021-11-01 14:59:35
Compiled: 2021-11-01 14:59:41

1 Getting started

#Load packages
library(EBImage)
library(rMiW)

2 Import a kidney image from rMiW

The displayed image (`Mouse01_Kid_x20_z0_RR01.png’) is an image of whole slide imaging (WSI) for observing the kidney tissue of C57BL/6J mouse (male, 10 week-old) stained by H&E.

2.1 Read image

file <- system.file("extdata", "Mouse01_Kid_x20_z0_RR01.png", package="rMiW")

#Read image
Img <- EBImage::readImage(files = file)
str(Img)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:1257, 1:1038, 1:3] 0.902 0.906 0.906 0.902 0.906 ...
##   ..@ colormode: int 2
##   ..$ dim: int [1:3] 1257 1038 3

Here, :: in R script indicates an explicit relation between the package and the functions.

2.2 Visualization

#Visualization
EBImage::display(Img, method = "raster")
Mouse01_Kid_x20_z0_RR01.png

Figure 1: Mouse01_Kid_x20_z0_RR01.png

3 Basic image processing

In this section, we will handle the Image object of EBIimage and perform image processing.

3.1 Convert to the grey image

#Read image: delete the 4th element of 3th dimension
#Img <- EBImage::readImage(files = file)

#Convert to the gray image
GrayImg <- toGrayScale(Img, mode = "luminance")

#Visualization
EBImage::display(GrayImg, method = "raster")
The gray image of Mouse01_Kid_x20_z0_RR01.png

Figure 2: The gray image of Mouse01_Kid_x20_z0_RR01.png

3.2 Resize the image

#1% area size
Img10 <- EBImage::resize(Img, 
                         w = round(dim(Img)[1]*0.1, 0), 
                         filter = "bilinear")

#6% area size
Img25 <- EBImage::resize(Img, 
                         w = round(dim(Img)[1]*0.25, 0), 
                         filter = "bilinear")

#25% area size
Img50 <- EBImage::resize(Img, 
                         w = round(dim(Img)[1]*0.5, 0), 
                         filter = "bilinear")

#50% area size
Img70 <- EBImage::resize(Img, 
                         w = round(dim(Img)[1]*0.707, 0), 
                         filter = "bilinear")

#Visualization
par(mfrow=c(2,2))
EBImage::display(Img10, method = "raster")
text(x = dim(Img10)[1]/20, y = dim(Img10)[2]/10, label = "1% area size", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Img25, method = "raster")
text(x = dim(Img25)[1]/20, y = dim(Img25)[2]/10, label = "6% area size", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Img50, method = "raster")
text(x = dim(Img50)[1]/20, y = dim(Img50)[2]/10, label = "25% area size", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Img70, method = "raster")
text(x = dim(Img70)[1]/20, y = dim(Img70)[2]/10, label = "50% area size", adj = c(0,0), col = "black", cex = 1.5)
The resized images of Mouse01_Kid_x20_z0_RR01.png

Figure 3: The resized images of Mouse01_Kid_x20_z0_RR01.png

3.3 Transpose the image

Here we will perform some rotation operations.

#Transpose the image
ImgTrans <- EBImage::transpose(Img)

#Flip or flop the image
Imgflip <- EBImage::flip(Img)
Imgflop <- EBImage::flop(Img)

#Visualization
par(mfrow=c(2,2))
EBImage::display(Img, method = "raster")
text(x = dim(Img)[1]/20, y = dim(Img)[2]/10, label = "Original", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(ImgTrans, method = "raster")
text(x = dim(ImgTrans)[1]/20, y = dim(ImgTrans)[2]/10, label = "Transpose", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Imgflip, method = "raster")
text(x = dim(Imgflip)[1]/20, y = dim(Imgflip)[2]/10, label = "Flip", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Imgflop, method = "raster")
text(x = dim(Imgflop)[1]/20, y = dim(Imgflop)[2]/10, label = "Flop", adj = c(0,0), col = "black", cex = 1.5)
The rotated images of Mouse01_Kid_x20_z0_RR01.png

Figure 4: The rotated images of Mouse01_Kid_x20_z0_RR01.png

  • Transpose : 90 degree rotation + Horizontal rotation (90度回転 +左右方向の回転)
  • Flip : Vertical rotation (上下方向の回転)
  • Flop : Horizontal rotation (左右方向の回転)

3.4 Save image

We can save the file in image format (jpeg, png, tiff) or R binary (.Rds).

#Save as PNG
EBImage::writeImage(Img, files = "./Img.png",
                    type = "png")

#Save as TIFF
EBImage::writeImage(Img, files = "./Img.tif",
                    type = "tiff")

#Save as R binary for single Objects (.Rda)
saveRDS(Img, "./Img.Rds")

#Read Rds format
ImgRds <- readRDS("./Img.Rds")
str(ImgRds)

When we save the object/variable in R as a Rds file and load it, the same R object will be reproduced.

4 Basic clustering using k-means

k-means clustering is an unsupervised clustering technique to partition N observations into k clusters in which each observation belongs to the cluster with the nearest mean.

4.1 The clustering with a compressed image of 1024x1024px

Here, we use the k-means clustering to divide the RGB intensity of the image into three classes.

#Read image: delete the 4th element of 3th dimension
#Load from the R binary
Img <- readRDS(system.file("extdata", "Mouse01_Kid_x20_z0_RR01.Rds", package="rMiW"))
str(Img)

#Resize 1024x1024 and perform 3 clustering 
ImgClus3 <- Img2DClustering(x=Img, Cluster = 3, XY=1024)

#Visualize as a color image
rasterMiW(ImgClus3, method = "raster")
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:1257, 1:1038, 1:3] 0.902 0.906 0.906 0.902 0.906 ...
##   ..@ colormode: int 2
##   ..$ dim: int [1:3] 1257 1038 3
Results of three clustering with k-means for Mouse01_Kid_x20_z0_RR01.png

Figure 5: Results of three clustering with k-means for Mouse01_Kid_x20_z0_RR01.png

Session information

## R version 4.1.1 (2021-08-10)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Catalina 10.15.7
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] BiocStyle_2.20.2
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.7          RColorBrewer_1.1-2  bslib_0.3.0        
##  [4] compiler_4.1.1      BiocManager_1.30.16 jquerylib_0.1.4    
##  [7] highr_0.9           base64enc_0.1-3     keras_2.6.0        
## [10] bitops_1.0-7        tools_4.1.1         zeallot_0.1.0      
## [13] digest_0.6.27       jsonlite_1.7.2      evaluate_0.14      
## [16] lattice_0.20-44     png_0.1-7           rlang_0.4.12       
## [19] Matrix_1.3-4        magick_2.7.3        yaml_2.2.1         
## [22] parallel_4.1.1      xfun_0.26           fastmap_1.1.0      
## [25] DiagrammeR_1.0.6.1  stringr_1.4.0       knitr_1.34         
## [28] generics_0.1.0      sass_0.4.0          fftwtools_0.9-11   
## [31] htmlwidgets_1.5.4   locfit_1.5-9.4      grid_4.1.1         
## [34] reticulate_1.22     glue_1.4.2          R6_2.5.1           
## [37] rMiW_0.99.1         jpeg_0.1-9          rmarkdown_2.11     
## [40] bookdown_0.24       purrr_0.3.4         whisker_0.4        
## [43] magrittr_2.0.1      tfruns_1.5.0        htmltools_0.5.2    
## [46] BiocGenerics_0.38.0 assertthat_0.2.1    abind_1.4-5        
## [49] colorspace_2.0-2    EBImage_4.34.0      tiff_0.1-8         
## [52] tensorflow_2.6.0    stringi_1.7.5       visNetwork_2.0.9   
## [55] RCurl_1.98-1.5